package com.tekj.cxqcb.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.orhanobut.logger.Logger;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import common.base.MainApplication;
import es.dmoral.toasty.Toasty;


public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final int RETURN_MSG_TYPE_LOGIN = 1;
    private static final int RETURN_MSG_TYPE_SHARE = 2;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.pay_result);
//        this.getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT);
        MainApplication.api = WXAPIFactory.createWXAPI(this, MainApplication.APP_ID);
//        MyApplication.WiexinAPI.registerApp(MyApplication.WeixinAppID);
        try {
            MainApplication.api.handleIntent(getIntent(), this);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        MainApplication.api.handleIntent(intent, this);
    }

    /**
     * description: 微信发送请求到第三方应用时，会回调到该方法
     * autour: tongzhenggang@126.com
     * date: 2017/10/25
     */
    @Override
    public void onReq(BaseReq req) {
        Logger.d("req>>>" + req.toString());

        Toast.makeText(this, "openid = " + req.openId, Toast.LENGTH_SHORT).show();

        switch (req.getType()) {
            case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
                Toast.makeText(this, "  COMMAND_GETMESSAGE_FROM_WX", Toast.LENGTH_SHORT).show();

                break;
            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
                Toast.makeText(this, "  COMMAND_SHOWMESSAGE_FROM_WX", Toast.LENGTH_SHORT).show();

                break;
            case ConstantsAPI.COMMAND_LAUNCH_BY_WX:
                Toast.makeText(this, "Launch From Weixin", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }


    /**
     * description: 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
     * autour: tongzhenggang@126.com
     * date: 2017/10/25
     */
    @Override
    public void onResp(BaseResp resp) {
        Logger.d("baseresp.getType = " + resp.getType() + "  resp.errCode = " + resp.errCode
                + "  resp.openId = " + resp.openId);
//        Toast.makeText(this, "baseresp.getType = " + resp.getType() + "  resp.errCode = " + resp.errCode
//                + "  resp.openId = " + resp.openId, Toast.LENGTH_SHORT).show();

        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                switch (resp.getType()) {
                    case RETURN_MSG_TYPE_LOGIN:
                        //拿到了微信返回的code,立马再去请求access_token
                        Toasty.success(getApplicationContext(), "微信授权成功").show();

                        String code = ((SendAuth.Resp) resp).code;
                        String state = ((SendAuth.Resp) resp).state;
                        Logger.d("code = " + code + "  state = " + state);
                        //发起网络请求
//                        initGetWxLoginInfo(code, state);
                        break;

                    case RETURN_MSG_TYPE_SHARE:
                        Toasty.success(getApplicationContext(), "微信分享成功").show();
                        finish();
                        break;
                }

                break;
            case BaseResp.ErrCode.ERR_COMM:
                Toasty.error(getApplicationContext(), "ERR_COMM").show();
                finish();
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                Logger.d("用户取消操作");
                Toasty.error(this, "用户取消操作").show();
                WXEntryActivity.this.finish();
//                if (RETURN_MSG_TYPE_SHARE == resp.getType()) {
//                    Toasty.error(getApplicationContext(), "分享失败").show();
//                } else {
//                    String state = ((SendAuth.Resp) resp).state;
//                    //第三方登录
//                    if (state.equals("wxlogin")) {
//                        Toasty.error(getApplicationContext(), "登录失败").show();
//                    }  //数据绑定
//                    if (state.equals("wxbinding")) {
//                        Toasty.error(getApplicationContext(), "绑定失败").show();
//                    }
//                    finish();
//                }
                break;
            case BaseResp.ErrCode.ERR_SENT_FAILED:
                Toasty.error(getApplicationContext(), "ERR_SENT_FAILED").show();
                finish();
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                Toasty.error(getApplicationContext(), "ERR_AUTH_DENIED").show();
                finish();
                break;
            case BaseResp.ErrCode.ERR_UNSUPPORT:
                Toasty.error(getApplicationContext(), "ERR_UNSUPPORT").show();
                finish();
                break;
            case BaseResp.ErrCode.ERR_BAN:
                Toasty.error(getApplicationContext(), "签名异常").show();
                finish();
                break;

            default:
                Logger.d("default");
                break;
        }
    }


    /**
     * description: 请求微信获取信息
     * autour: tongzhenggang@126.com
     * date: 2017/10/25
     */
//    private void initGetWxLoginInfo(final String code, final String state) {
//        Logger.d("initGetWxLoginInfo>>>code=" + code + "  state=" + state);
//        new GetWeixinLoginData(new OkGoFinishListener() {
//            @Override
//            public void onSuccess(String mStr, Object bean) {
//                GetWeixinLoginData.ReturnOkBean mBean = (GetWeixinLoginData.ReturnOkBean) bean;
//
//                if (mBean.getExpires_in() == 7200) {
//                    String openid = mBean.getOpenid();
//                    String unionid = mBean.getUnionid();
//                    Logger.d("openid>>>" + openid);
//
////                        Toasty.error(getApplicationContext(), "unionid>>> " + unionid).show();
//                } else {
//                    Toasty.error(getApplicationContext(), "WXEntryActivity>>>微信授权异常").show();
//                }
//                finish();
//
//            }
//        }, getApplicationContext(), code);
//
//
//
//    }


}